#include "TcpConnection.hpp"
#include <sstream>

namespace wd
{

TcpConnection::TcpConnection(int fd)
: _sock(fd)
, _sockIO(fd)
, _localAddr(getLocalAddress())
, _peerAddr(getPeerAddress())
{

}

string TcpConnection::receive()
{
    char buff[65536] = {0}; // 在栈上设置 64KB 空间
    int ret = _sockIO.readline(buff , sizeof(buff));

    // 设置第二个参数 ret ，是为了防止字符串内容中间有'\0'
    return string(buff , ret);
}

void TcpConnection::send(const string & msg)
{
    if(msg.size() > 0)
    {
        _sockIO.sendn(msg.c_str() , msg.size());
    }
}

// 判断一个连接是否断开了
bool TcpConnection::isClosed() const
{
    char buff[20] = {0};
    return _sockIO.recvPeek(buff , sizeof(buff)) == 0;
}

void TcpConnection::shutdown()
{
    _sock.shutdownWrite();
}

string TcpConnection::toString() const
{
    std::ostringstream oss;
    oss << "TCP" << _localAddr.ip() << " : " << _localAddr.port()
        << " -> " << _peerAddr.ip() << " : " << _peerAddr.port();

    return oss.str();
}

InetAddress TcpConnection::getLocalAddress()
{
    struct sockaddr_in addr;
    memset(&addr , 0 , sizeof(addr));
    socklen_t len = sizeof(addr);
    int ret = getsockname(_sock.fd() , (struct sockaddr *)&addr , &len);

    if(ret < 0)
    {
        perror("getsockname");
    }

    return InetAddress(addr);
}

InetAddress TcpConnection::getPeerAddress()
{
    struct sockaddr_in addr;
    memset(&addr , 0 , sizeof(addr));
    socklen_t len = sizeof(addr);
    int ret = getpeername(_sock.fd() , (struct sockaddr *)&addr , &len);

    if(ret < 0)
    {
        perror("getpeername");
    }

    return InetAddress(addr);
}

} // end of namespace wd
